
class JQProductAttrCreateService extends global.JQService {
    async do_action(){
		
		if(!this.conn)console.log('do_action::conn error');
		let data_obj = this.req_obj.data_obj;
		let inst = global.JQTableIF.instance('cp_attribution_table',this.conn);
		
		// _addParams
		
		
		if(!data_obj || !data_obj || !data_obj.obj.attr_name){
			throw {'code':global.consts.Error.ERROR_PARAM,
				'message':global.consts.Message.ERROR_PARAM};
		}
		
		/*
		'id',// int(11) AI PK
		'attr_name',// varchar(64) 
		'attr_price',// double(20,2) 
		'attr_type',// int(11) 
		'cp_id',// int(11) 
		'attr_vip_price',// double(20,2) new added
		'attr_status',//int(11)     new added
		
		'sort', // new added
		*/
		if(data_obj.obj.cp_id){
			let params = super._addParams(['attr_name=',data_obj.obj.attr_name]);
			 params = super._addParams(['attr_type=',data_obj.obj.attr_type],params);
			 params = super._addParams(['attr_status=',1],params);
			params = super._addParams(['cp_id=',data_obj.obj.cp_id],params);
			let one = await inst.queryOne(null,params,null,'sort desc');
			if(one){
				
				return one;
			}
		}
		
		
		let obj = null;
		if(data_obj && data_obj.obj && data_obj.obj.id){
			obj= data_obj.obj;
		}
		if(!obj){
			obj= {};
			if(data_obj.obj.cp_id){
				
				let params = super._addParams(['attr_type=',data_obj.obj.attr_type]);
				params = super._addParams(['cp_id=',data_obj.obj.cp_id],params);
				params = super._addParams(['attr_status=',1],params);
				let one = await await inst.queryOne(['max(sort) sort'],params,null,null);
				let sort =1;
				if(one){
					if(one && one.sort)sort = one.sort+1;
				}
				obj.sort=sort;
				obj.cp_id=data_obj.obj.cp_id;
			}
			
			obj.attr_name=data_obj.obj.attr_name;
			obj.attr_type=data_obj.obj.attr_type;
			
			
		}
		let result = await inst.create(obj);	
		
		if(!result)return null;
		let wrap ={obj:result};		
		return wrap;
			
	}

}
module.exports = JQProductAttrCreateService;

